package com.homwee.factory;

import java.util.ArrayList;
import java.util.List;


import com.homwee.listMenu.ListMenu;
import com.homwee.listMenu.util.MenuItemData;
import com.homwee.listMenu.util.MenuListView;
import com.homwee.tvap.common.ap.ComSettingManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;
import com.mediatek.twoworlds.factory.common.MtkTvFApiDisplayTypes;
import com.mediatek.twoworlds.factory.common.MtkTvFApiDisplayTypes.ColorTempData;

import com.homwee.factory.tvtool.ReproductionService;
import com.homwee.factory.tvtool.NewPQActivity;
import com.homwee.factory.tvtool.AQService;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class TvToolService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	static WindowManager mWindowManager;
	LayoutInflater mInflater;
	LayoutParams wmParams_main;
	MenuListView mListView;
	public ComSettingManager mcomSettingManager;
	public FactorySetting factorySetting;
	MenuItemData ColorTempMode;

	protected List<MenuItemData> mListItemData;
	private ListMenu mListMenu;


	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		mWindowManager = (WindowManager) getApplication().getSystemService(
				Context.WINDOW_SERVICE);
		mInflater = LayoutInflater.from(getApplication());
		mcomSettingManager = ComSettingManager
				.getInstance(TvToolService.this);
		factorySetting = mcomSettingManager.getFactory();
		createMainView();
		mWindowManager.addView(mListMenu, wmParams_main);
	}

	private void createMainView() {
		wmParams_main = new LayoutParams();
		wmParams_main.type = LayoutParams.TYPE_PHONE;
		wmParams_main.format = PixelFormat.RGBA_8888;
		wmParams_main.gravity = Gravity.LEFT | Gravity.TOP;
		wmParams_main.x = 0;
		wmParams_main.y = 0;
		wmParams_main.width = LayoutParams.WRAP_CONTENT;
		wmParams_main.height = LayoutParams.MATCH_PARENT;
		mListItemData = new ArrayList<MenuItemData>();
		List<MenuItemData> mListItemDatas = loadItemData();
		mListItemData.get(0).mTitleName = "TvTool";
		mListMenu = new ListMenu(this);
		mListMenu.init(mListItemDatas, 0, 0);

		mListMenu.setVersion(this.getString(R.string.factory_software_version)
				+ factorySetting.getSWVersion());
		mListMenu.setVersionDate(this.getString(R.string.factory_comfile_time)
				+ factorySetting.getSWDate());
		mListMenu.setOnTitleOnkeyListener(new ListMenu.OnTitleOnkeyListener() {
			@Override
			public void onKey(int keyCode) {
				if(keyCode==4) {
					mWindowManager.removeView((View) mListMenu);
					stopSelf();
					Intent mIntent = new Intent();
					mIntent.setComponent(new ComponentName("com.homwee.factory",
							"com.homwee.factory.FactoryActivity"));
					mIntent.addFlags(mIntent.FLAG_ACTIVITY_NEW_TASK);
					mIntent.putExtra("PageIndex", 0);
					mIntent.putExtra("ItemIndex", 0);
					TvToolService.this.startActivity(mIntent);
				}
			}
		});
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	public List<MenuItemData> loadItemData() {
		mListItemData.clear();

		MenuItemData AQItem = new MenuItemData("AQ");
		AQItem.setOnEnterKey(new MenuItemData.onEnterRightKey() {

			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				Log.v("tv", "AQ onEnterRightKey");
				Intent AQServiceIntent = new Intent(
			    TvToolService.this, AQService.class);
				startService(AQServiceIntent);
				mWindowManager.removeView((View) mListMenu);
				stopSelf();
			}
		});
		
		MenuItemData PQItem = new MenuItemData("PQ");
		PQItem.setOnEnterKey(new MenuItemData.onEnterRightKey() {

			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				Log.v("tv", "PQ onEnterRightKey");
				Intent PQServiceIntent = new Intent(
						TvToolService.this, NewPQActivity.class);
				PQServiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
				startActivity(PQServiceIntent);
				mWindowManager.removeView((View) mListMenu);
				stopSelf();
			}
		});
		
		MenuItemData DispalyRateItem = new MenuItemData("Dispaly Rate");
		DispalyRateItem.setOnEnterKey(new MenuItemData.onEnterRightKey() {

			public void onEnterRightKey() {
				// TODO Auto-generated method stub
				Log.v("tv", "Dispaly Rate onEnterRightKey");
				Intent ReproductionServiceIntent = new Intent(
						TvToolService.this, ReproductionService.class);
				startService(ReproductionServiceIntent);
				mWindowManager.removeView((View) mListMenu);
				stopSelf();
			}
		});



		AQItem.setOnBackKey(backKey);
		PQItem.setOnBackKey(backKey);
		DispalyRateItem.setOnBackKey(backKey);


		mListItemData.add(AQItem);
		mListItemData.add(PQItem);
		mListItemData.add(DispalyRateItem);


		return mListItemData;
	}

	MenuItemData.onEnterBackKey backKey = new MenuItemData.onEnterBackKey() {

		@Override
		public void onEnterBackKey() {
			// TODO Auto-generated method stub
			mWindowManager.removeView((View) mListMenu);
			stopSelf();
			Intent mIntent = new Intent();
			mIntent.setComponent(new ComponentName("com.homwee.factory",
					"com.homwee.factory.FactoryActivity"));
			mIntent.addFlags(mIntent.FLAG_ACTIVITY_NEW_TASK);
			mIntent.putExtra("PageIndex", 0);
			mIntent.putExtra("ItemIndex", 0);
			TvToolService.this.startActivity(mIntent);
		}
	};



}
